CIS 554 Prolog Quiz--Answer Key |
Name ______________________________________ |
Remember that capitalization is important in Prolog. If you use incorrect capitalization, or write so that I cannot tell whether a word is capitalized, it's wrong.
Write Prolog predicates to express each of the following:
(Remember that a predicate consists of one or more clauses.)
is_divisible_by(X, Y) :- X is Y * (X / Y).
to write a predicate
to test whether a year is a leap year. The rule is: A year is a leap year if (1) it is divisible by 4, and (2) is it not divisible by 100, unless (3) it is also divisible by 400. Thus, 2000 was a leap year, but 1900 was not.mod is an infix binary operator. Here are some other ways you might write is_divisible_by. For each, mark if it is correct or incorrect.is_divisible_by(X, Y) :- X mod Y = 0. incorrect is_divisible_by(X, Y) :- 0 = X mod Y. incorrectis_divisible_by(X, Y) :- X mod Y is 0.incorrectis_divisible_by(X, Y) :- 0 is X mod Y. correct Unification and Logic
[ace, deuce, trey] = [H | T] H = ace, T = [deuce, trey] [Apple, pear] = [banana | Grape] Apple = banana, Grape = [pear] ¬on_budget(X) ∨ ¬on_time(X) ∨ good_product(X).
¬good_product(Y)
loves(chuck, X) :- female(X), rich(X).
¬female(X) ∨ ¬rich(X) ∨ loves(chuck, X)